Ext.define('Oa.controller.ClientController',{
    extend:'Ext.app.Controller',
    stores:['ClientStore','ClientComboStore'],
    models:[],
    views:[
        'client.ShowPanel',
        'client.EditPanel'
    ],
    init:function(){
        var record = null;
        var id = null;
        this.control({
            'clientshow>grid':{
                render:function(){
                    var store = Ext.getCmp('clientGrid').store;
                    store.on('beforeload', function (store, options) {
                        Ext.apply(store.proxy.extraParams, {'name':null,"val":null});
                    });
                    store.load();
                },
                selectionchange:function(model, records,obj){
                     if (records[0]) {
                        record = records[0];
                        id = records[0].raw.id;
                        Ext.getCmp('del').setDisabled(false);
                        Ext.getCmp('update').setDisabled(false);
                    }
                }
            },
            'clientshow textfield':{
                keyup:function(){
                    var name = Ext.getCmp('clientName').value;
                    var val = Ext.getCmp('combo').getSubmitValue();
                    var valName =Ext.getCmp('combo').getRawValue( );
                    var store = Ext.getCmp('clientGrid').store;
                    if (val!=null) {
                        store.on('beforeload', function (store, options) {
                            Ext.apply(store.proxy.extraParams, {'name':name,"val":val});
                        });
                        store.load({
                            callback:function(){
                                if(store.data.get(0)==null||store.data.get(0)==""){
                                    Ext.MessageBox.alert("提示","没有找到条件为"+valName+"的“"+name+"”");
                                }
                            }
                        });
                    }
                },
                focus:function(obj){
                    obj.reset();
                }
            },
            'clientshow combo':{
                select:function(){
                    var name = Ext.getCmp('clientName').value;
                    var val = Ext.getCmp('combo').getSubmitValue();
                    var valName =Ext.getCmp('combo').getRawValue( );
                        var store = Ext.getCmp('clientGrid').store;
	                    store.on('beforeload', function (store, options) {
	                        Ext.apply(store.proxy.extraParams, {'name':name,"val":val});
	                    });
	                    store.load({
	                        callback:function(){
	                            if(store.data.get(0)==null||store.data.get(0)==""){
	                                Ext.MessageBox.alert("提示","没有找到条件为"+valName+"的“"+name+"”");
	                            }
	                        }
	                    });
                }
            },
            'clientshow button[action = update]':{
                click:function(){
                    var obj = Ext.getCmp('clientedit');
                    var window = null;
                    if(obj!=undefined&&obj!=null){
                        var form = Ext.getCmp('clientedit').down('form').getForm();
                        form.loadRecord(record);
                         Ext.getCmp('clientedit').show();               
                    }else{
                        window = Ext.create('Oa.view.client.EditPanel');
                        var form = window.down('form').getForm();
                        form.loadRecord(record);
                        window.show();
                    }
                }
            },
            'clientshow button[action = all]':{
                click:function(){
                    var store = Ext.getCmp('clientGrid').store;
                    store.on('beforeload', function (store, options) {
                        Ext.apply(store.proxy.extraParams, {'name':null,"val":null});
                    });
                    store.load();
                    Ext.getCmp('del').setDisabled(true);
                    Ext.getCmp('update').setDisabled(true);
                }
            },
            'clientedit button[action = com]':{
                click:function(obj){
                        //提交
                        var form = obj.up('form').getForm();
                        if (form.isValid()) {
                            Ext.Ajax.request({
	                            url:'../clientAction/addOrUpdateClient.do',
	                            params:form.getValues(true),
	                            success:function(){
	                                Ext.MessageBox.alert("提示","操作成功！",function(obj){Ext.getCmp('clientedit').close();});
                                    Ext.getCmp('clientGrid').setDisabled(false);
	                                Ext.getCmp('clientGrid').store.load();
                                    Ext.getCmp('update').setDisabled(true);
			                        Ext.getCmp("clientedit").getForm().reset();
			                        Ext.getCmp("del").setDisabled(true);
	                            },
	                            failure:function(form,action){
	                                Ext.MessageBox.alert("错误！","操作失败<br>服务报告了一项错误，或连接失败")
	                            }
                            })
                    }
                }
            },
            'clientshow button[action = add]':{
                click:function(){
                    var obj = Ext.getCmp('clientedit');
                    var window = null;
                    if(obj!=undefined&&obj!=null){
                         Ext.getCmp('clientedit').show();               
                    }else{
                        window = Ext.create('Oa.view.client.EditPanel');
                        window.show();
                    }
                }
            },
            'clientedit button[action = cancel]':{
                click:function(obj){
                    obj.up('clientedit').close();
                }
            },
            'clientshow button[action = del]':{
                click:function(obj){
                    Ext.MessageBox.show({
                                title:'警告',
                                msg:'该操作不可逆转!<br>确定要删除吗？',
                                buttons:Ext.Msg.YESNO,
                                icon:Ext.Msg.WARNING,
                                fn:function(is){
                                    if(is=='yes'){
                                        Ext.Ajax.request({
			                                url:'../clientAction/delClient.do',
			                                params:{"id":id},
			                                success:function(){
			                                    Ext.MessageBox.alert("提示","删除成功！");
                                                obj.setDisabled(true);
                                                Ext.getCmp('update').setDisabled(true);
                                                Ext.getCmp('clientGrid').store.load();
			                                },
			                                failure:function(){
			                                    Ext.MessageBox.alert("错误！","操作失败<br>服务报告了一项错误，或连接失败")
			                                }
		                                 })                                   
                                    }
                            }
                        });
                }
            }
        })
    }
})